package com.intelligent.ispc.web.utils;

//import org.apache.batik.anim.dom.SAXSVGDocumentFactory;
import org.apache.batik.dom.svg.SVGDocumentFactory;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.io.*;
import java.util.Map;

/**
 * Created by Peter on 2016/1/6.
 */
public class SvgPngConverter {
//    public static void convertToPngByFile(String filePath, String pngFilePath, Map<String, String> map) throws IOException, TranscoderException {
//        File file = new File(pngFilePath);
//        FileOutputStream outputStream = null;
//
//        try {
//            file.createNewFile();
//            outputStream = new FileOutputStream(file);
//            convertToPngByFile(filePath, outputStream, map);
//        } finally {
//            if (outputStream != null) {
//                try {
//                    outputStream.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//    }
//
//    public static void convertToPngByFile(String path, OutputStream outputStream, Map<String, String> map) throws TranscoderException, IOException {
//        try {
//            File file = new File(path);
//            String parser = XMLResourceDescriptor.getXMLParserClassName();
////            SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
//
////            JPEGTranscoder t = new JPEGTranscoder();
//
////            Document doc = t.createDocument(file.toURI().toString());
//
////            for (int i = 1; i <= map.size() / 3; i++) {
////                Element e = doc.getElementById(map.get("id" + i));
////                System.out.println(map.get("name" + i));
////                e.setAttribute(map.get("name" + i), map.get("value" + i));
////            }
//
//            PNGTranscoder t = new PNGTranscoder();
//            TranscoderInput input = new TranscoderInput(doc);
//            TranscoderOutput output = new TranscoderOutput(outputStream);
//            t.transcode(input, output);
//            outputStream.flush();
//        } finally {
//            if (outputStream != null) {
//                try {
//                    outputStream.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//    }


    public static void convertToPng(String svgCode, String pngFilePath) throws IOException, TranscoderException {
        File file = new File(pngFilePath);
        FileOutputStream outputStream = null;

        try {
            file.createNewFile();
            outputStream = new FileOutputStream(file);
            convertToPng(svgCode, outputStream);
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void convertToPng(String svgCode, OutputStream outputStream) throws TranscoderException, IOException {
        try {
            byte[] bytes = svgCode.getBytes("UTF-8");
            PNGTranscoder t = new PNGTranscoder();
            TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(bytes));
            TranscoderOutput output = new TranscoderOutput(outputStream);
            t.transcode(input, output);
            outputStream.flush();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
